//
//  MBSpaceCollectionCompileView.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceCollectionCompileView.h"

@interface MBSpaceCollectionCompileView () {
    UIView                  *bgView;
}

@property (nonatomic,assign)NSInteger   selectIndex;

@end
@implementation MBSpaceCollectionCompileView

- (id)initWithFrame:(CGRect)frame
{
   return [self initWithFrame:frame WithIndex:3];
}

- (id)initWithFrame:(CGRect)frame WithIndex:(NSInteger)index
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectIndex = index;
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    float itemSpace = 20*PERCEN;
    float itemWidth = 1. * (bgView.width - (self.selectIndex+1)*itemSpace)/self.selectIndex;
    if (ISIPAD) {
        itemWidth = 360*PERCEN*3./self.selectIndex;
    }
    for (int i=0; i<self.selectIndex; i++) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemSpace+(itemWidth+itemSpace)*i, 18*PERCEN, itemWidth, 64*PERCEN);
        bt.backgroundColor = [UIColor whiteColor];
        [bt setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        [bt setTitleColor:UIColorFromHex(0x999999) forState:UIControlStateDisabled];
        bt.layer.cornerRadius = 6.f;
        bt.layer.masksToBounds = YES;
        bt.layer.borderColor = UIColorFromHex(0x999999).CGColor;
        bt.layer.borderWidth = 1;
        bt.tag = 2000+i;
        bt.titleLabel.font = [UIFont systemFontOfSize:13.];
        [bt addTarget:self action:@selector(onTapForMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:bt];
        
        if (i == 0) {
            [bt setTitle:@"取消" forState:UIControlStateNormal];
        }
        else if (i == 1) {
            [bt setTitle:@"删除" forState:UIControlStateNormal];
            bt.enabled = NO;
        }
        else if (i == 2) {
            [bt setTitle:@"取消收藏" forState:UIControlStateNormal];
            bt.enabled = NO;
        }
        
        if (ISIPAD) {
            bt.left = 440*PERCEN+(itemWidth+44*PERCEN)*i;
        }
    }
    
}

- (void)onUpdateDataForDelegateAndCollection:(BOOL)isEnabled
{
    UIButton *deleteBt = (UIButton *)[self viewWithTag:2001];
    UIButton *collectionBt = (UIButton *)[self viewWithTag:2002];
    
    deleteBt.enabled = isEnabled;
    collectionBt.enabled = isEnabled;
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    
    bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    float itemSpace = 20*PERCEN;
    float itemWidth = 1. * (bgView.width - (self.selectIndex+1)*itemSpace)/self.selectIndex;
    if (ISIPAD) {
        itemWidth = 360*PERCEN*3./self.selectIndex;
    }
    for (int i=0; i<self.selectIndex; i++) {
        UIButton *bt = (UIButton *)[bgView viewWithTag:2000+i];
        bt.frame = CGRectMake((bgView.width-itemWidth*self.selectIndex-itemSpace*(self.selectIndex-1))/2.+(itemWidth+itemSpace)*i, 18*PERCEN, itemWidth, 64*PERCEN);
    }
}

- (void)onTapForMusicAction:(UIButton *)bt
{
    NSLog(@"取消、删除、收藏");
    switch (bt.tag-2000) {
        case 0:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForCancel:)]) {
                [_delegate onSelectItemForCancel:self];
            }
        }
            break;
        case 1:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForDelete:)]) {
                [_delegate onSelectItemForDelete:self];
            }
        }
            break;
        case 2:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForNoCollection:)]) {
                [_delegate onSelectItemForNoCollection:self];
            }
        }
            break;
            
        default:
            break;
    }
}

@end
